using System;
using System.ComponentModel.Design;
using System.IO;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;

namespace PrismFactory.SoftwareFactory.Actions.Temp
{
    [ServiceDependency(typeof (ITypeResolutionService))]
    public sealed class TextTemplateAction : T4Action
    {
        // Fields

        // Methods
        public override void Execute()
        {
            string template = Template;
            string templateCode = string.Empty;
            if (template == null)
            {
                throw new ArgumentNullException("Template");
            }
            string templateBasePath = base.GetTemplateBasePath();
            if (!Path.IsPathRooted(template))
            {
                template = Path.Combine(templateBasePath, template);
            }
            template = new FileInfo(template).FullName;
            if (!template.StartsWith(templateBasePath))
            {
                throw new ArgumentException("TextTemplateAction_TemplatePathNotInPackageFolder");
            }
            templateCode = File.ReadAllText(template);
            Content = base.Render(templateCode, template);
        }

        public override void Undo()
        {
        }

        // Properties
        [Output]
        public string Content { get; set; }

        [Input(true)]
        public string Template { get; set; }
    }
}